﻿using System.Threading;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;



namespace SISSA.Test.Can
{

    public class Program
    {
        static int  T1, T2, BRP;
        
        
        /// <summary>
        /// Initialisation des composants
        /// </summary>
        private static void Initialize()
        {
            //KEYPad LCD Initialize
            FEZ_Shields.KeypadLCD.Initialize();
            FEZ_Shields.KeypadLCD.TurnBacklightOn();


            //CAN
            /////////////////////////////////////////////////////////////////////////////////////////////
            // Bitrate 125Kbps
            // CLK = 72 Mhz, with BRP = 24 -> 3Mhz CAN clock
            // 3Mhz/125Kbps = 24 TQ
            // T1 = 16 minus 1 for sync = 15
            // T2 = 8
            // 15 + 1 + 8 = 24 TQs which is what we need
            /////////////////////////////////////////////////////////////////////////////////////////////
            // uncomment to use this bit timing
            BRP = 24;
            T1 = 15;
            T2 = 8;

        }

            
        
        public static void Main()
        {
            

            
            Initialize();

            // Use channel 1
            CAN can = new CAN(CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));

            while (true)
            {
                FEZ_Shields.KeypadLCD.Clear();
                FEZ_Shields.KeypadLCD.CursorHome();
                FEZ_Shields.KeypadLCD.Print("Tagada");
                //Attend pression d'une touche
                FEZ_Shields.KeypadLCD.Keys PressedKey = FEZ_Shields.KeypadLCD.GetKey();
                if (PressedKey != FEZ_Shields.KeypadLCD.Keys.None)
                {
                    
                    FEZ_Shields.KeypadLCD.Print("Sending Message");
                    SendCanMessage(can);
                    FEZ_Shields.KeypadLCD.Print(" - Message Sent");
                    
               
                }

                FEZ_Shields.KeypadLCD.Print("Attente reception :"); 
                int received= TestReceiveMessage(can, 5000);
                if (received >= 0)
                {
                    FEZ_Shields.KeypadLCD.Print("Recu :" + received);
                    Thread.Sleep(2000);
                }
                
                Thread.Sleep(1);
            }
        }

        private static int TestReceiveMessage(CAN can,uint time)
        {
            uint i = 0;
            uint pause = 10; //pause de 10ms
            int receivedCount = 0;
            CAN.Message[] msgList = new CAN.Message[] {};
            while (i < time)
            {
                if (can.GetRxQueueCount() == 0)
                {
                    Thread.Sleep(10);
                    i = i + pause;
                }
                else
                    i = time;
            }
            receivedCount = can.GetMessages(msgList);
            return receivedCount;
        }

        private static void SendCanMessage(CAN can)
        {
            // This is the message
            CAN.Message message = new CAN.Message();

            // We only use one message, more can be added.
            CAN.Message[] msgList = new CAN.Message[] { message };

            // Random data
            message.data[0] = 1;
            message.data[1] = 2;
            message.data[2] = 3;
            message.data[3] = 4;
            message.data[4] = 5;
            message.data[5] = 6;
            message.data[6] = 7;
            message.data[7] = 8;

            message.DLC = 8;
            message.ArbID = 0x55;
            message.isEID = true;
            message.isRTR = false;

            // send a message
            int sentCount;
            if (can.IsTxBufferAvailable())
                sentCount = can.PostMessages(msgList);
        }

    }
}  
